package com.usian.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.entity.Notifications;
import com.usian.order.feign.CarFeignClient;
import com.usian.order.mapper.NotificationsMapper;
import com.usian.order.service.INotificationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.vo.NotificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@Service
public class NotificationsServiceImpl extends ServiceImpl<NotificationsMapper, Notifications> implements INotificationsService {

    @Autowired
    private CarFeignClient carFeignClient;

    @Override
    public Result<NotificationsVo> NotificationsAll(Integer userId) {
        LambdaQueryWrapper<Notifications> wrapper = new LambdaQueryWrapper<>();
        List<Notifications> notificationsList = this.list(wrapper.eq(Notifications::getUserId, userId));
        if (notificationsList.isEmpty()){
            return Result.build(null, ResultCodeEnum.DATA_NOT_EXISTS);
        }
        List<NotificationsVo> notificationsVos = new ArrayList<>();
        for (Notifications notifications : notificationsList) {
            Result<String> carImagesByCarId = carFeignClient.getCarImagesByCarId(notifications.getCarId());
            NotificationsVo notificationsVo = new NotificationsVo();
            notificationsVo.setImageUrl(carImagesByCarId.getData());
            notificationsVo.setMessageText(notifications.getMessageText());
            notificationsVo.setCreatedAt(notifications.getCreatedAt());
            notificationsVos.add(notificationsVo);
        }
        return Result.build(notificationsVos, ResultCodeEnum.SUCCESS);
    }
}
